<template>
  <div class="game-lobby">
    <!-- 背景特效 -->
    <div class="particles-background"></div>

    <!-- 主网格布局 -->
    <div class="grid-container">
      <!-- 用户信息区域 -->
      <div class="user-panel">
        <UserProfile />
      </div>

      <!-- 游戏展示区域 -->
      <div class="games-panel">
        <div class="games-grid">
          <GameCard
              v-for="game in games"
              :key="game.id"
              :game="game"
              @click="selectGame(game)"
          />
        </div>
      </div>

      <!-- 聊天区域 -->
      <div class="chat-panel">
        <ChatWindow />
      </div>

      <!-- 排行榜区域 -->
      <div class="leaderboard-panel">
        <Leaderboard />
      </div>
    </div>

    <!-- 移动端底部导航栏 -->
    <div class="mobile-nav">
      <button class="mobile-nav-btn" @click="togglePanel('user')">
        <span class="mobile-nav-icon">👤</span>
        <span>个人中心</span>
      </button>
      <button class="mobile-nav-btn" @click="togglePanel('leaderboard')">
        <span class="mobile-nav-icon">🏆</span>
        <span>排行榜</span>
      </button>
      <button class="mobile-nav-btn" @click="togglePanel('chat')">
        <span class="mobile-nav-icon">💬</span>
        <span>聊天</span>
      </button>
    </div>
  </div>
</template>

<script setup>
import {onMounted, ref} from 'vue'
import UserProfile from '../UserProfile.vue'
import ChatWindow from '../ChatWindow.vue'
import Leaderboard from '../Leaderboard.vue'
import GameCard from '../GameCard.vue'
import { useGames } from '../../composables/useGame'
import {getAllGames} from "@/api/game.js";

const { games, selectGame } = useGames()

onMounted(() => {
  getAllGames().then((response)=>{
    games.value = response
  }).catch((error) => {
    console.error('获取游戏列表失败:', error)
  })
})

// 移动端面板控制
const activePanel = ref(null)

const togglePanel = (panel) => {
  // 隐藏所有面板
  document.querySelectorAll('.user-panel, .chat-panel, .leaderboard-panel').forEach(el => {
    el.classList.remove('mobile-panel-active')
  })

  if (activePanel.value === panel) {
    activePanel.value = null
  } else {
    activePanel.value = panel
    document.querySelector(`.${panel}-panel`).classList.add('mobile-panel-active')
  }
}
</script>

<style scoped>
.game-lobby {
  position: relative;
  min-height: 100vh;
  width: 100%;
  overflow-x: hidden;
  background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
}

.particles-background {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
  background-size: cover;
  opacity: 0.3;
}

.grid-container {
  display: flex;
  flex-direction: column;
  gap: 15px;
  padding: 15px;
  height: 100%;
  position: relative;
  z-index: 1;
}

/* 移动端默认隐藏侧边栏 */
.user-panel,
.chat-panel,
.leaderboard-panel {
  display: none;
}

.games-panel {
  background: rgba(22, 33, 62, 0.7);
  border-radius: 12px;
  backdrop-filter: blur(10px);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.3);
  border: 1px solid rgba(255, 255, 255, 0.1);
  padding: 15px;
  height: 90vh;
  overflow-y: auto;
  flex: 1;
}

.games-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 12px;
  padding: 5px;
}

/* 移动端底部导航栏 */
.mobile-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  background: rgba(26, 26, 46, 0.95);
  backdrop-filter: blur(10px);
  display: flex;
  justify-content: space-around;
  padding: 10px 0;
  z-index: 100;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
}

.mobile-nav-btn {
  color: #fff;
  background: transparent;
  border: none;
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.mobile-nav-btn.active {
  background: rgba(52, 152, 219, 0.3);
}

.mobile-nav-icon {
  font-size: 18px;
}

/* 移动端侧边栏激活状态 */
.mobile-panel-active {
  background: rgba(6, 16, 43, 0.95);
  display: block !important;
  position: fixed;
  top: 0;
  left: 0;
  width: 80%;
  height: 100%;
  z-index: 101;
  animation: slideIn 0.3s ease-out;
}

@keyframes slideIn {
  from { transform: translateX(-100%); }
  to { transform: translateX(0); }
}

/* 平板设备适配 */
@media (min-width: 768px) {
  .grid-container {
    display: grid;
    grid-template-columns: 250px 1fr;
    grid-template-rows: auto 1fr;
    grid-template-areas:
      "user games"
      "leaderboard games";
    gap: 15px;
  }

  .user-panel,
  .leaderboard-panel {
    display: block;
  }

  .user-panel {
    grid-area: user;
  }

  .leaderboard-panel {
    grid-area: leaderboard;
  }

  .games-panel {
    grid-area: games;
  }

  .games-grid {
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  }

  .mobile-nav {
    display: none;
  }
}

/* 桌面设备适配 */
@media (min-width: 1024px) {
  .grid-container {
    grid-template-columns: 300px 1fr 300px;
    grid-template-rows: auto 1fr;
    grid-template-areas:
      "user games chat"
      "leaderboard games chat";
  }

  .chat-panel {
    display: flex;
    grid-area: chat;
  }

  .games-grid {
    grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
    gap: 20px;
  }
}

/* 超大屏幕适配 */
@media (min-width: 1440px) {
  .games-grid {
    grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  }
}

/* 横屏模式优化 */
@media screen and (max-width: 767px) and (orientation: landscape) {
  .games-grid {
    grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  }

  .mobile-nav {
    padding: 6px 0;
  }

  .mobile-nav-btn {
    padding: 6px 8px;
    font-size: 11px;
  }

  .mobile-nav-icon {
    font-size: 16px;
  }
}
</style>
